package com.biz.primus.app.rest.controller.base;

import com.biz.primus.app.rest.handler.BuildRequestHandler;
import com.biz.primus.base.support.rest.vo.JsonResult;
import com.biz.primus.base.exception.BizException;
import com.biz.primus.base.global.GlobalParams;
import com.biz.primus.base.global.GlobalRefHandler;
import com.biz.primus.common.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Validation;
import javax.validation.Validator;
import java.util.ArrayList;
import java.util.List;

/**
 * 控制器基类,要求所有面向手机端的api都要继承此类
 *
 * @author jun.liu
 * @since 2017年11月3日
 * @usage
 * @reviewer
 */
@RestController
@Slf4j
public abstract class RestBaseController {

	private final static Validator VALIDATOR = Validation.buildDefaultValidatorFactory().getValidator();

	public final static String POST_DATA_PARAM_NAME = "data";
	@Autowired
	private List<BuildRequestHandler> handlers = new ArrayList<>();

//	/**
//	 * @param request
//	 *            http请求
//	 * @param clazz
//	 *            请求vo的class
//	 * @param validationGroups
//	 *            用于jsr303验证的group class
//	 * @return
//	 */
//	protected <V extends IRequestVo> V buildVo(HttpServletRequest request, Class<V> clazz, Class<?>... validationGroups) {
//		String bizData = request.getParameter(POST_DATA_PARAM_NAME);
//		log.debug("请求参数:\n{}", bizData);
//		V vo = null;
////		try {
////			if (StringUtils.isNotBlank(bizData)) {
////				// 有业务参数, 使用json反序列化
////				vo = JsonUtil.json2Obj(bizData, clazz);
////			} else {
////				// 无业务参数, 默认构建
////				vo = clazz.newInstance();
////			}
////		} catch (Exception e) {
////			log.error("解析接口业务参数出错", e);
////			throw new BusinessException(Exceptions.Global.PARAMETER_ERROR, e);
////		}
////
////		Class<?>[] groups = null;
////		if (validationGroups != null && validationGroups.length > 0) {
////			groups = validationGroups;
////		}
////		try {
////			validate(vo, groups);
////		} catch (ValidationException e) {
////			Throwable cause = e.getCause();
////			if (cause instanceof BusinessException) {
////				throw (BusinessException) cause;
////			}
////			throw e;
////		}
////		for (BuildRequestHandler handler : handlers) {
////			handler.handle(request, vo);
////		}
////
//		return vo;
//	}
//

	/**
	 * 构建请求
	 * @param req
	 * @param gp
	 * @param <T>
	 * @return
	 */
	protected <T> T buildRequest(GlobalParams gp,T req){
		//从全局参数获取对应引用
		GlobalRefHandler.handle(gp, req);
		if (log.isDebugEnabled()) {
			log.debug("请求参数:\n{}", JsonUtils.obj2Json(req));
		}
		return req;
	}

	@ExceptionHandler
	public JsonResult exceptionHandler(Exception ex){
		log.error("api exception:",ex);
		if(ex instanceof BizException){
			BizException bizException = (BizException)ex;
			return new JsonResult(bizException.getCode(),bizException.getDescription());
		}
		return new JsonResult(900,"server error");
	}




}
